//
//  HDCoverageTools.m
//  myWeiKuaidi
//
//  Created by 黄通际 on 2022/6/28.
//  Copyright © 2022 kuaibao. All rights reserved.
//

#import "HDCoverageTools.h"
#ifndef PROFILE_INSTRPROFILING_H_
#define PROFILE_INSTRPROFILING_H_

// https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
int __llvm_profile_runtime = 0;
void __llvm_profile_initialize_file(void);
const char *__llvm_profile_get_filename(void);
void __llvm_profile_set_filename(const char *);
int __llvm_profile_write_file(void);
int __llvm_profile_register_write_file_atexit(void);
const char *__llvm_profile_get_path_prefix(void);
#endif /* PROFILE_INSTRPROFILING_H_ */

@implementation HDCoverageTools
+ (void)load {
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(didEnterBackgroundNotification) name:NSExtensionHostDidEnterBackgroundNotification object:nil];
}

+ (void)didEnterBackgroundNotification {
    [self saveAndUpload];
}

+ (void)saveAndUpload {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];//获取app版本信息
    NSString *targetName = [infoDictionary objectForKey:@"CFBundleExecutable"];
    NSLog(@"CFBundleExecutable: %@",targetName);
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    NSString *filePath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.profraw", targetName]];
    NSLog(@"filePath: %@", filePath);
    __llvm_profile_set_filename(filePath.UTF8String);
    __llvm_profile_write_file();
}

@end
